package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.*;
import com.wisewin.circle.service.GroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/group")
public class GroupController extends BaseController {

    @Autowired
    private GroupService groupService;

    // 添加拼团
    @PostMapping("/addGroup")
    public ResultDTO addGroup (HttpServletRequest request, @RequestBody GroupDO groupDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupDO.setUserId(loginUser.getId());
        return groupService.addGroup(groupDO);
    }

    // 参加拼团
    @PostMapping("/joinGroup")
    public ResultDTO joinGroup (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.joinGroup(groupOrderDO);
    }
    
    // 重新支付
    @PostMapping("/payGroupOrder")
    public ResultDTO payGroupOrder (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.payGroupOrder(groupOrderDO);
    }

    // 重新支付修改订单地址
    @PostMapping("/updateGroupOrderAddress")
    public ResultDTO updateGroupOrderAddress (HttpServletRequest request, @RequestBody GroupOrderAddressDO groupOrderAddressDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        return groupService.updateGroupOrderAddress(groupOrderAddressDO);
    }

    // 结束拼团
    @PostMapping("/endGroup")
    public ResultDTO endGroup (HttpServletRequest request, @RequestBody GroupDO groupDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupDO.setUserId(loginUser.getId());
        return groupService.endGroup(groupDO);
    }

    // 查询我的拼团我发起的
    @PostMapping("/queryUserGroupList")
    public ResultDTO queryUserGroupList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        Integer userId = queryParamBO.getUserId() == null ? loginUser.getId() : queryParamBO.getUserId();
        queryParamBO.setUserId(userId);
        return groupService.queryUserGroupList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询我的拼团我参与的
    @PostMapping("/queryUserGroupJoinList")
    public ResultDTO queryUserGroupJoinList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        Integer userId = queryParamBO.getUserId() == null ? loginUser.getId() : queryParamBO.getUserId();
        queryParamBO.setUserId(userId);
        return groupService.queryUserGroupJoinList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()), loginUser.getId());
    }
    
    // 查询我参与的拼团详情
    @PostMapping("/queryUserGroupOrderDetail")
    public ResultDTO queryUserGroupOrderDetail (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.queryUserGroupOrderDetail(groupOrderDO);
    }

    // 添加拼团评论
    @PostMapping("/addGroupComment")
    public ResultDTO addGroupComment (HttpServletRequest request, @RequestBody GroupCommentDO groupCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupCommentDO.setCommentUserId(loginUser.getId());
        return groupService.addGroupComment(groupCommentDO);
    }

    // 删除拼团评论
    @PostMapping("/deleteGroupComment")
    public ResultDTO deleteGroupComment (HttpServletRequest request, @RequestBody GroupCommentDO groupCommentDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupCommentDO.setCommentUserId(loginUser.getId());
        return groupService.deleteGroupComment(groupCommentDO);
    }

    // 点赞拼团评论
    @PostMapping("/addGroupCommentHeart")
    public ResultDTO addGroupCommentHeart (HttpServletRequest request, @RequestBody GroupCommentHeartDO groupCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupCommentHeartDO.setUserId(loginUser.getId());
        return groupService.addGroupCommentHeart(groupCommentHeartDO);
    }

    // 取消拼团评论点赞
    @PostMapping("/cancleGroupCommentHeart")
    public ResultDTO cancleGroupCommentHeart (HttpServletRequest request, @RequestBody GroupCommentHeartDO groupCommentHeartDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupCommentHeartDO.setUserId(loginUser.getId());
        return groupService.cancleGroupCommentHeart(groupCommentHeartDO);
    }

    // 查询拼团评论列表
    @PostMapping("/queryGroupCommentList")
    public ResultDTO queryGroupCommentList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return groupService.queryGroupCommentList(queryParamBO);
    }

    // 查询拼团详情
    @PostMapping("/queryGroupDetail")
    public ResultDTO queryGroupDetail (HttpServletRequest request, @RequestBody GroupDO groupDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupDO.setUserId(loginUser.getId());
        return groupService.queryGroupDetail(groupDO.getGroupId(), loginUser.getId());
    }

    // 查询拼团参加人列表
    @PostMapping("/queryGroupJoinList")
    public ResultDTO queryGroupJoinList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return groupService.queryGroupJoinList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询拼团列表
    @PostMapping("/queryGroupList")
    public ResultDTO queryGroupList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(!StringUtils.isEmpty(token)){
            // 获取登录用户
            LoginUser loginUser = super.getLoginUser(token);
            if (loginUser == null ) { // 判断是否登录
                return ResultDTOBuilder.failure("0000001", "登录已过期", null);
            }
            queryParamBO.setUserId(loginUser.getId());
        }
        return groupService.queryGroupList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 关闭订单
    @PostMapping("/closeGroupOrder")
    public ResultDTO closeGroupOrder (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.closeGroupOrder(groupOrderDO);
    }

    // 确认拼团
    @PostMapping("/confirmGroupOrder")
    public ResultDTO confirmGroupOrder (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.confirmGroupOrder(groupOrderDO);
    }

    // 取消订单
    @PostMapping("/cancleGroupOrder")
    public ResultDTO cancleGroupOrder (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.cancleGroupOrder(groupOrderDO);
    }

    // 删除订单
    @PostMapping("/deleteGroupOrder")
    public ResultDTO deleteGroupOrder (HttpServletRequest request, @RequestBody GroupOrderDO groupOrderDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        groupOrderDO.setUserId(loginUser.getId());
        return groupService.deleteGroupOrder(groupOrderDO);
    }
}